<?php
/*code here*/
/* Redirect user if they already logged */
$db = new Database();
$fs = new functions();
if(Auth::hasAuth('current_user'))
{
    $auth = Auth::getAuth('current_user');
    //$this->view->current_user = Auth::getAuth('current_user');
    if($_GET['type']=="go"){
        $location = $_GET['redirect'];
        
        $redirect = str_replace("|=%=|", "&", $location);
        // Allow User View
        if(ALLOW_USER_ADMIN != "1"){
            if(ALLOW_USER_VIEW=="1"){
                if($auth['user_level_id']=="3"){
                    if($_GET['type']=="go"){
                        header('Location: ' . USER_VIEW .$redirect);
                    }else{
                        header('location: ' . USER_VIEW . 'home');
                    }
                    
                }else{
                    
                   header('Location: ' . MAIN_PAGE . $redirect);
                }
            }else{
                header('Location: ' . MAIN_PAGE .$redirect);
            }
        }else{
            if(ALLOW_USER_VIEW=="1"){
                if($auth['user_level_id']=="2"){
                    if($_GET['type']=="go"){
                        header('Location: ' . USER_VIEW .$redirect);
                    }else{
                        header('location: ' . USER_VIEW . 'home');
                    }
                    
                }else{
                    
                   header('Location: ' . MAIN_PAGE . $redirect);
                }
            }else{
                header('Location: ' . MAIN_PAGE .$redirect);
            }
        }
        
    }else{
        // Allow User View
        if(ALLOW_USER_ADMIN != "1"){
            if(ALLOW_USER_VIEW=="1"){
                if($auth['user_level_id']=="3"){
                     header('location: ' . USER_VIEW . 'home');
                }else{
                    header('location: /home');
                }
            }else{
                header('location: /home');
            }
        }else{
            if(ALLOW_USER_VIEW=="1"){
                if($auth['user_level_id']=="2" && ALLOW_USER_ADMIN == "1" || $auth['user_level_id']=="3" && ALLOW_USER_ADMIN == "1"){
                     header('location: ' . USER_VIEW . 'home');
                }else{
                    header('location: /home');
                }
            }else{
                header('location: /home');
            }
        }
    }
}
//remove application cache
//setcookie('application','');
/* For user login if correct redirect to home */
if($_SERVER['REQUEST_METHOD'] == "POST")
{
    $username = $_POST['username'];
    $password = $_POST['password'];
    $coords = $_POST['coords'];
    $session = new Auth();
    $login = $session->login($username,$password,'email','password','tbuser');
   
    if($login){
        // if($login['user_level_id']==2){
        //     setcookie('application','0');
        // }
        $date = $fs->currentDateTime();
        // Audit Logs
        $insert_audit_rec = array("user_id"=>$login['id'],
                                  "audit_action"=>"2",
                                  "table_name"=>"tbuser",
                                  "record_id"=>$login['id'],
                                  "date"=>$date,
                                  "ip"=>$_SERVER["REMOTE_ADDR"],
                                  "is_active"=>1);
        $audit_log = $db->insert("tbaudit_logs",$insert_audit_rec);
        
        // Save location of user logged in
            
            $insert = array("location"	=>	$coords,
                            "date"		=>	$date,
                            "userID"	=>	$login['id'],
                            "is_active"	=>	1);
            $db->insert("tblocation",$insert);

        
        $personDoc = new Person($db,$login['id']);
        $personDoc->is_available = '1';
        $personDoc->update();
        
        // Reset Timeout idle
        $personDoc = new Person($db,$login['id']);
        $personDoc->timeout = '0';
        $personDoc->update();
        
        if($_GET['type']=="go"){
            $location = $_GET['redirect'];
            $redirect = str_replace("|=%=|", "&", $location);
            // Allow User View
            if(ALLOW_USER_ADMIN != "1"){
                if(ALLOW_USER_VIEW=="1"){
                    if($login['user_level_id']=="3"){
                        if($_GET['type']=="go"){
                            header('Location: ' . USER_VIEW .$redirect);
                        }else{
                            functions::userLevelAuth_v2($login['user_level_id']);
                        }
                    }else{
                        header('Location: ' . MAIN_PAGE .$redirect);
                    }
                }else{
                    header('Location: ' . MAIN_PAGE .$redirect);
                }
            }else{
                if(ALLOW_USER_VIEW=="1"){
                    if($login['user_level_id']=="2" && ALLOW_USER_ADMIN == "1" || $login['user_level_id']=="3" && ALLOW_USER_ADMIN == "1"){
                        if($_GET['type']=="go"){
                            header('Location: ' . USER_VIEW .$redirect);
                        }else{
                            functions::userLevelAuth_v2($login['user_level_id']);
                        }
                    }else{
                        header('Location: ' . MAIN_PAGE .$redirect);
                    }
                }else{
                    header('Location: ' . MAIN_PAGE .$redirect);
                }
            }
        }else{
            
            $current_page = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            
            $getPage = $db->query("SELECT * FROM page_settings WHERE userID={$db->escape($login['id'])} AND is_active={$db->escape(1)}","row");
            $getPage_count = $db->query("SELECT * FROM page_settings WHERE userID={$db->escape($login['id'])} AND is_active={$db->escape(1)}","numrows");
            if($getPage_count==0){
                // Allow User View
                if(ALLOW_USER_ADMIN != "1"){
                    if(ALLOW_USER_VIEW=="1"){
                        if($login['user_level_id']=="3"){
                            functions::userLevelAuth_v2($login['user_level_id']);
                        }else{
                            header('location: /');
                        }
                    }else{
                        header('location: /');
                    }
                }else{
                    if(ALLOW_USER_VIEW=="1"){
                        if($login['user_level_id']=="2" && ALLOW_USER_ADMIN == "1" || $login['user_level_id']=="3" && ALLOW_USER_ADMIN == "1"){
                            //functions::userLevelAuth_v2($login['user_level_id']);
                            header('location: /');
                        }else{
                            header('location: /');
                        }
                    }else{
                        header('location: /');
                    }
                }
            }else{
                header('location: ' . $getPage['page']);
            }
        }
    }else{
        if($_GET['type']=="go"){
            $path = functions::curPageURL("");
            $get_module_param = explode("?",$path);
            header('Location: /login?' . $get_module_param[1] . '?' . $get_module_param[2] . '&login_attempt=1');
        }else{
            header('Location: /login?login_attempt=1'); 
        }
    }
}   
/*code here(end)*/
$this->setLayout();